const serviceSql=require('../service/service')
const {CODE_NULL}=require('../error/errorType')
const {v4}=require('uuid')

class convertController{
    async convert(ctx,next){
        const {id}=ctx.tokenInfo
        const code=(()=>v4())()
        let {affectedRows}=await serviceSql.cdkeyUpdata(ctx.code,id)
        if(!affectedRows){
            return ctx.app.emit('error',new Error(CODE_NULL),ctx)
        }
        await serviceSql.cdkeyInsert(code)
        ctx.amount=5
        ctx.codeStatus=1
        await next()
    }
}

module.exports=new convertController()